package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.TwoProblem;
import cn.pugle.oj.catalog.Unknown;

/**
 * https://leetcode.com/problems/remove-nth-node-from-end-of-list/
 * 错误写法, 要移除的是倒数第n个...
 *
 * @author tzp
 * @since 2020/10/20
 */
public class LC19 implements TwoProblem {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) return null;
        ListNode pre = null, cur = head;
        int i = 1;
        while (i <= n - 1) {//删除第3个, 要做2次移动
            pre = cur;
            cur = cur.next;
            i++;
        }
        pre.next = cur.next;
        return head;
    }

    public static void main(String[] args) {
        ListNode a = ListNode.arrayToListNode(new int[]{1, 2, 3, 4, 5});
        System.out.println(new LC19().removeNthFromEnd(a, 3));
    }
}
